// pages/action/action.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollindex: 0,
    totalnum: 4,
    starty: 0, 
    startTime: 0,
    endy: 0, 
    endTime: 0, 
    critical: 80,
    maxTimeCritical: 300,
    minTimeCritical: 100,
    margintop: 0,
    currentTarget: null,
  },

  toggle_up(e){
    wx.pageScrollTo({
      scrollTop: e.touches[0].pageY - 664,
      duration: 700
    })
  },
  toggle_down(e){
    wx.pageScrollTo({
      scrollTop: e.touches[0].pageY - 30,
      duration: 700
    })
  },

  scrollTouchStart: function(e) {
    let py = e.touches[0].pageY,
        stamp = e.timeStamp,
        currentTarget = e.currentTarget.id;
    // console.log(py);
    this.setData({
        starty: py,
        startTime: stamp,
        currentTarget: currentTarget
    })
  },

  scrollTouchMove(e) {
      // console.log(e);
  },

  scrollTouchEnd: function(e) {
    let py = e.changedTouches[0].pageY,
        stamp = e.timeStamp,
        d = this.data,
        timeStampdiffer = stamp - d.startTime;
    this.setData({
        endy: py,
        endTime: stamp
    })

    if (timeStampdiffer <= d.maxTimeCritical && timeStampdiffer > d.minTimeCritical && (d.starty > e.changedTouches[0].pageY)) {
        let currentTarget = parseInt(d.currentTarget.slice(4)),
            nextTarget = currentTarget + 1;
        const query = wx.createSelectorQuery();
        query.select(`#hook${nextTarget}`).boundingClientRect();
        query.selectViewport().scrollOffset();
        query.exec(function (res) {
            // console.log(res);
            if (res[0] != null) {
                wx.pageScrollTo({
                    scrollTop: res[0].height * currentTarget,
                })
            }
        })
    } else if (timeStampdiffer <= d.maxTimeCritical && timeStampdiffer > d.minTimeCritical && (d.starty < e.changedTouches[0].pageY)) {   // 下拉
        let currentTarget = parseInt(d.currentTarget.slice(4)),
            preTarget = currentTarget - 2 == -1 ? 0 : currentTarget - 2;
    　　const query = wx.createSelectorQuery();
    　　query.select(`#hook1`).boundingClientRect();
    　　query.selectViewport().scrollOffset();
    　　query.exec(function (res) {
            // console.log(res);
            wx.pageScrollTo({
                scrollTop: res[0].height * preTarget
        　　})
        })
    }        
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})